package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.DruSupplier;
import com.ruoyi.project.drug.service.DruDrugService;
import com.ruoyi.project.drug.service.DruSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Transactional
@RequestMapping("/drug/supplier")
public class DruSupplierController extends BaseController {

    @Autowired
    private DruSupplierService druSupplierService;


    /**
     * 根据条件查询供应商信息
     *
     * @param druSupplier  用于传送查询条件
     * @return  返回查询结果集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:selectAll')")
    @GetMapping("/selectAll")
    public TableDataInfo selectAll(DruSupplier druSupplier) {

        startPage();

        List<DruSupplier> list = druSupplierService.selectAll(druSupplier);

        return getDataTable(list);
    }



    /**
     * 无条件查询所有供应商信息
     *
     * @return  返回查询结果集合
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:selectAll')")
    @GetMapping("/selectSupplier")
    public AjaxResult selectSupplier() {
        return AjaxResult.success(druSupplierService.selectSupplier());
    }



    /**
     * 添加供应商信息
     *
     * @param druSupplier  用来传递供应商数据信息
     * @return  返回 1 新增车工  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:addSupplier')")
    @PostMapping("/addSupplier")
    public AjaxResult addSupplier(@RequestBody DruSupplier druSupplier) {

        druSupplier.setCreateBy(SecurityUtils.getUsername());
        druSupplier.setCreateTime(new Date());

        return toAjax(druSupplierService.addSupplier(druSupplier));
    }



    /**
     * 根据主键查询单个的供应商信息
     *
     * @param dsid  供应商主键ID
     * @return  返回查询的单挑供应商信息
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:updateSupplier')")
    @GetMapping("/selectOne/{dsid}")
    public AjaxResult selectOne(@PathVariable int dsid) {
        return AjaxResult.success(druSupplierService.selectOne(dsid));
    }



    /**
     * 修改供应商信息
     *
     * @param druSupplier  用来传递供应商数据信息
     * @return  返回 1 修改车工  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:updateSupplier')")
    @PutMapping("/updateSupplier")
    public AjaxResult updateSupplier(@RequestBody DruSupplier druSupplier) {

        druSupplier.setUpdateBy(SecurityUtils.getUsername());
        druSupplier.setUpdateTime(new Date());

        return toAjax(druSupplierService.updateSupplier(druSupplier));
    }



    /**
     * 根据主键ID删除单条或多条供应商信息
     *
     * @param dsids  供应商ID数组
     * @return  返回 1 删除车工  否则失败
     */
    @PreAuthorize("@ss.hasAnyPermi('drug:supplier:deleteSupplier')")
    @DeleteMapping("/deleteSupplier/{dsids}")
    public AjaxResult deleteSupplier(@PathVariable int[] dsids) {
        return toAjax(druSupplierService.deleteSupplier(dsids));
    }
}
